package com.sysu.ooad.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class AjaxCommand {
	public static void ajaxReturnJSON(HttpServletResponse response, Object obj)
			throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		JSONObject json = JSONObject.fromObject(obj);
		out.println(json.toString());
		System.out.println(json.toString());
		out.close();
	}
	
	public static void ajaxReturnJSON(HttpServletResponse response, Object obj, JsonConfig cfg)
			throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		JSONObject json = JSONObject.fromObject(obj, cfg);
		out.println(json.toString());
		System.out.println(json.toString());
		out.close();
	}

	public static void ajaxReturnJSONArray(HttpServletResponse response,
			Object obj) throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		JSONArray json = JSONArray.fromObject(obj);
		out.println(json.toString());
		System.out.println(json.toString());
		out.close();
	}

	public static void ajaxReturnJSONArray(HttpServletResponse response,
			Object obj, JsonConfig cfg) throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		JSONArray json = JSONArray.fromObject(obj, cfg);
		out.println(json.toString());
		System.out.println(json.toString());
		out.close();
	}
}
